﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using DevExpress.Web.ASPxEditors;
using System.Data;
using Frame.Global;
using DevExpress.Web.ASPxGridView;

public partial class EditUser : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        //Frame.Global.GlobalFunctions.SetGridView(gridView, true);
        InitComboBox(gridView.Columns["t_Community_ID"], "Community", "Community_Name", "Community_ID", "Parent_ID");
        
    }
    protected void LSMDSourceMain_Selecting(object sender, DevExpress.Data.Linq.LinqServerModeDataSourceSelectEventArgs e)
    {
        DAL.DataClassesDataContext lqDB = new DataClassesDataContext();
        e.QueryableSource = lqDB.Users.Where(o => o.IsAdmin == 0).AsQueryable();
    }
    public static void InitComboBox(GridViewColumn Column, string TableName, string TextField, string ValueField, string Parent_ID)
    {
        DataTable table = new DataTable();
        GridViewDataComboBoxColumn column = Column as GridViewDataComboBoxColumn;
        foreach (DataRow row in GlobalSqlHelper.ExecuteSQLToDataTable(string.Format("select {0},{1} from {2} where {3}=0", TextField, ValueField, TableName, Parent_ID)).Rows)
        {
            column.PropertiesComboBox.Items.Add(new ListEditItem(row[TextField].ToString(), row[ValueField].ToString()));
        }
    }
    protected void gridView_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        e.NewValues["IsAdmin"] = 0;
    }

    protected void gridView_RowCommand(object sender, ASPxGridViewRowCommandEventArgs e)
    {
        using (DataClassesDataContext lqDB = new DataClassesDataContext())
        {
            int id = Convert.ToInt32(e.KeyValue);
            Users type = lqDB.Users.FirstOrDefault(t => t.ID == id);
            lqDB.Users.DeleteOnSubmit(type);
            lqDB.SubmitChanges();
            Frame.Global.GlobalFunctions.AlertToUrl("删除成功！", "EditUser.aspx");
        }
    }
}
